package tienda.servlet;

import java.io.IOException;
import java.sql.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import tienda.exception.DAOExcepcion;
import tienda.negocio.GestionarPersona;
import tienda.modelo.Persona;;


/**
 * Servlet implementation class RegistrarPersona
 */
@WebServlet("/RegistrarPersona")
public class RegistrarPersona extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegistrarPersona() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub}
		try{
			
			GestionarPersona negCan = new GestionarPersona();
					
			String a=request.getParameter("txtNombre");
			String b=request.getParameter("txtApePaterno");
			String c=request.getParameter("txtApeMaterno");
			String d=request.getParameter("cboSexo");
			String e=request.getParameter("txtEmail");
			//Date f = Date.valueOf(request.getParameter("txtFecNacimiento"));
			String f = request.getParameter("txtFecNacimiento");
			
			String g=request.getParameter("txUsuario");
			String h=request.getParameter("txtPassword");
			Integer i=Integer.parseInt(request.getParameter("cbotipopersona"));			
			String j=request.getParameter("txtNumCelular");
			String k=request.getParameter("cboTipDocumento");
			
			String m=request.getParameter("txtNroDocumento");		
				
			try {
				Persona vo=negCan.insertar(a, b, c, d, e, f, g, h, i, j, k, m);
						if(vo.getNombres()!=null && vo.getNombres()!="")	
						{
							request.setAttribute("MENSAJE","Se registro la persona");						
							
						}else
						{
							
							request.setAttribute("MENSAJE","No se pudo registrar la persona, probablemente ya exista");							
						}
						

				
			} catch (DAOExcepcion e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				request.setAttribute("MENSAJE", e1.getMessage());
			}
		
			
		} catch (IllegalArgumentException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			request.setAttribute("MENSAJE", "ERROR, Falta llenar campos o tipo de variable no soportada: -" + e2.getMessage() + "-");
	}
		
			RequestDispatcher rd = request
					.getRequestDispatcher("RegistrarPersona.jsp");
			rd.forward(request, response);
		
		
	}

}
